<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
	function __construct()
	{
		parent::Controller();
		$this->load->model('login/login_model');
		//$this->load->language('login/login', $this->_lang);
	}
	
	public function index()
	{
		$data = array();
		$uid = '1';
		$this->update_all_res_user($uid);
		$rInfo = $this->run_time_res($uid);
		$data['rfood'] = $rInfo[0];
		$data['rwood'] = $rInfo[1];
		$data['rmetal'] = $rInfo[2];
		$data['rpetroleum'] = $rInfo[3];
		$res = $this->attack_model->ww_get_res_user_info($uid);
		$data['res'] = $res;
		// header
		
		// content
		$this->load->view('login/login_view', $data);
		
		// foter
	}
	function run_time_res($uid)
	{
		/*
		chia cho 1 phut
		*/
		$food = 60000/($this->_food/60);
		$wood = 60000/($this->_wood/60);
		$metal = 60000/($this->_metal/60);
		$petroleum = 60000/($this->_petroleum/60);
		
		$result = array($food, $wood, $metal, $petroleum);
		return $result;
	}
	function update_all_res_user($uid)
	{
		$data = array();
		$food = $this->_food;
		$wood = $this->_wood;
		$metal = $this->_metal;
		$petroleum = $this->_petroleum;
		$time = time();
		$res = $this->attack_model->ww_get_res_user_info($uid);
		
		$lastupdate = $res['lastupdate'];
		
		$valFloat = (((($time - $lastupdate)/60)/60)/2);
		$food = floor($valFloat*$food);
		$wood = floor($valFloat*$wood);
		$metal = floor($valFloat*$metal);
		$petroleum = floor($valFloat*$petroleum);
		if($food >= 1 || $wood >= 1 || $metal >= 1 || $petroleum >= 1)
		{
			$update_res_by_hour = $this->attack_model->ww_update_res_by_hour($uid, $food, $wood, $metal, $petroleum, $time);
		}
	}
}